package com.yx.cloud.view;


import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatEditText;

public class SearchEditText extends AppCompatEditText {

    public SearchEditText(@NonNull Context context) {
        super(context);
    }

    public SearchEditText(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public SearchEditText(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch(event.getAction()){
            case MotionEvent.ACTION_DOWN:
                Drawable right = getCompoundDrawables()[2];//右侧的drawable
                if (right != null && event.getRawX() >= (getRight() - right.getBounds().width()) - 60) {//获取点击事件的触发范围
                    rightListener.onDrawableRightClick(this);//事件回调
                    return true ;
                }
                break;
        }
        return super.onTouchEvent(event);

    }

    private RightListener rightListener;

    public void setRightListener(RightListener rightListener){
        this.rightListener = rightListener;
    }

    public interface RightListener{
        void onDrawableRightClick(View view);
    }
}
